package dao;

import model.Trade;
import model.TradeType;

import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Dao_type extends Dao{
    private static List tradeTypes;


    //获取所有商品类别
    public static List selectAll()
    {
        List list = new ArrayList();
        String sql = "select * from commodity_sort ";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                TradeType tradeType = new TradeType();

                tradeType.setType_name(re.getString("cs_name"));
                tradeType.setType_id(re.getString("cs_no"));
                tradeType.setMax(re.getInt("up_limit"));
                tradeType.setMin(re.getInt("low_limit"));

                list.add(tradeType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //删除
    public static int deleteType(String typeID) {
        int i = 0;
        try {
            String sql = "delete from  commodity_sort where cs_no = '" + typeID + "'";
            i = Dao.exeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Dao.close();

        return i;
    }

    //修改
    public static int updateType(String typeID, String type, int max, int min)
    {
        int i = 0;

        try {
            String sql = "update  commodity_sort set cs_no = '" + typeID + "',cs_name = '" + type + "', up_limit = " + max + ", low_limit=" + min + " where cs_no = '" + typeID + "'";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception E)
        {
            E.printStackTrace();
        }

        return i;
    }

    public static int add(String typeID, String type, int max, int min)
    {
        int i = 0;

        try {
            String sql = "insert into commodity_sort(cs_no, cs_name, low_limit, up_limit) values('"+ typeID +"','"+ type +"',"+ min +","+ max +")";
            i = Dao.exeUpdate(sql);
        }

        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //获取所有商品类型编号
    public static Vector<String> getTypeID()
    {
        List list = Dao_type.selectAll();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            TradeType type = (TradeType) list.get(i);
            WareID.add(type.getType_id());
        }

        return WareID;
    }

    //通过商品类型ID获取商品类型名称
    public static String getTypeID_type(String typeID)
    {
        if(tradeTypes == null) tradeTypes = Dao_type.selectAll();

        for(int i = 0; i < tradeTypes.size(); i++)
        {
            TradeType type = (TradeType) tradeTypes.get(i);

            if(typeID.equals( type.getType_id())){
                return type.getType_name();
            }
        }

        return null;
    }

    //通过商品类型ID获取上限
    public static double getTypeID_max(String tradeID) {
        if(tradeTypes == null)
            tradeTypes = Dao_type.selectAll();

        for (int i = 0; i < tradeTypes.size(); i++) {
            TradeType trade = (TradeType)  tradeTypes.get(i);

            if (tradeID.equals(trade.getType_id()))

                return trade.getMax();
        }

        return 0;
    }

    //通过商品类型ID获取下限
    public static double getTypeID_min(String tradeID) {
        if(tradeTypes == null)
            tradeTypes = Dao_type.selectAll();

        for (int i = 0; i < tradeTypes.size(); i++) {

            TradeType trade = (TradeType)  tradeTypes.get(i);

            if (tradeID.equals(trade.getType_id()))

                return trade.getMin();
        }

        return 0;
    }
}
